<template>
  <div>
    <el-dialog :visible="addDialog" title="新增学科" width="400px" @close="onClose">
      <el-form ref="form" :model="tageForm" :rules="rules">
        <el-form-item v-if="!this.$route.query.id" label="所属学科">
          <el-select v-model="tageForm.subjectID" placeholder="请选择">
            <el-option
                v-for="item in subjectsSimple"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model="tageForm.tagName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onClose()">取 消</el-button>
        <el-button type="primary" @click="tagsAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { addTags } from '../../api/hmmm/tags'

export default {
  name: 'addTags',
  props: {
    addDialog: {
      type: Boolean,
      required: true
    },
    subjectsSimple: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      tageForm: {
        subjectID: '',
        tagName: ''
      },
      //  验证规则
      rules: {
        tagName: [
          { required: true, message: '请输入要添加的目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 关闭弹框事件
    onClose () {
      this.$refs.form.resetFields()
      this.tageForm = {}
      this.$emit('update:addDialog', false)
    },
    // 弹框提交事件
    async tagsAdd () {
      try {
        await this.$refs.form.validate()
        if (this.$route.query.id) {
          await addTags({
            tagName: this.tageForm.tagName,
            subjectID: this.$route.query.id * 1
          })
          this.$message.success('添加成功')
          this.$emit('update:addDialog', false)
          this.$emit('loadDirectorsList')
        } else {
          await addTags(this.tageForm)
          this.$message.success('添加成功')
          this.$emit('update:addDialog', false)
          this.$emit('loadDirectorsList')
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang='less' scoped>
.el-form {
  padding-bottom: 10px;

  .el-input {
    width: 200px;
  }
}
</style>
